package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.beans.Person;
import com.bussiness.AdminBussinessProxy;
import com.bussiness.MovieBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;

/**
 * Servlet implementation class MovieReportServlet
 */
public class MovieReportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminBussinessProxy adminBI = new AdminBussinessProxy(); 
	MovieBussinessProxy movieBI = new MovieBussinessProxy();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MovieReportServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		RequestDispatcher dispatcher;
		Movie[] movie = null;
		Movie m =null;
		String[] ids;
		
		try
		{
			String catId = request.getParameter("category");
			String input = request.getParameter("inputstring");
			String type = request.getParameter("typeid");
			System.out.println("Category Id:"+ catId);
			System.out.println("Input String:"+ input);
			System.out.println("type:"+ type);
			adminBI.setEndpoint(ServiceEndPoints.AdminEndPoint);
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			if(catId.equals("1")){
				m = adminBI.getMovieByName(input);
//				movie.setMovieId(m.getMovieId());
//				movie.setMovieName(m.getMovieName());
//				movie.setMovieBanner(m.getMovieBanner());
//				movie.setReleaseDate(m.getReleaseDate());
//				movie.setRent(m.getRent());
//				movie.setCategoryid(m.getCategoryid());
//				movie.setAvailableCopy(m.getAvailableCopy());
//				movie.setRentamount(m.getRentamount());
//				movie.setIssuedtomembers(m.getIssuedtomembers());
				movie = new Movie[1];
				movie[0]=m;
			}
			if(catId.equals("2")){
				m = adminBI.getMovieByBanner(input);
				movie = new Movie[1];
				movie[0]=m;
				
			}
			if(catId.equals("3")){
				movie = adminBI.getMovieByRent(Double.parseDouble(input));
			}
			if(catId.equals("type")){
				System.out.println("in catid type:"+type);
				movie = adminBI.getMovieByCategory(type);
				
			}
			if(catId.equals("5")){
				movie = adminBI.getMovieByReleaseDate(input);
			}
			
			
			session.setAttribute("selectedMovies", movie);
				
			dispatcher = getServletContext().getRequestDispatcher("/moviereport.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			session.setAttribute("ServerMessage", UIMessages.SERVICE_UNAVAILABLE);
			dispatcher = getServletContext().getRequestDispatcher("/moviereport.jsp");
			dispatcher.forward(request, response);
		}
	

	
	}

}
